package com.leyou.user.sercice;

import com.leyou.common.utils.CodecUtils;
import com.leyou.common.utils.NumberUtils;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String key_prefix = "user:verify:";

    /** 
    * @Description: 校验数据是否可用 
    * @Param:
    * @return:
    * @Author: XinWei Wu
    * @Date: 2020/6/14 
    */
    public Boolean checkUser(String data, Integer type) {
        User record = new User();
        if (type == 1){
            record.setUsername(data);
        }else if (type == 2){
            record.setPhone(data);
        }else {
            return null;
        }
        return this.userMapper.selectCount(record) == 0;
    }

    public void sendVerifyCode(String phone) {
        if (StringUtils.isBlank(phone)){
            return;
        }

        //生成验证码
        String code = NumberUtils.generateCode(6);

        Map<String,String> msg = new HashMap<>();
        msg.put("phone",phone);
        msg.put("code",code);

        //发送消息到rabbitmq
        this.amqpTemplate.convertAndSend("leyou.sms.exchange","verifycode.sms",msg);

        //把验证码保存到redis

        this.redisTemplate.opsForValue().set(key_prefix + phone,code,5, TimeUnit.MINUTES);

    }

    public Boolean register(User user, String code) {
        //校验短信验证码
        String cacheCode = this.redisTemplate.opsForValue().get(key_prefix + user.getPhone());
        if (!StringUtils.equals(code,cacheCode)){
            return false;
        }
        //生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);


        //对密码加密
        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));

        //强制设置不能置顶的参数为null
        user.setId(null);
        user.setCreated(new Date());
        //添加到数据库
        boolean b = this.userMapper.insertSelective(user) == 1;
        //注册成功，删除redis中的记录
        if (b){
            this.redisTemplate.delete(key_prefix + user.getPhone());
        }
        return b;
    }

    public User queryUser(String username, String password) {
        // 查询
        User record = new User();
        record.setUsername(username);
        User user = this.userMapper.selectOne(record);
        // 校验用户名
        if (user == null) {
            return null;
        }
        // 校验密码
        if (!user.getPassword().equals(CodecUtils.md5Hex(password, user.getSalt()))) {
            return null;
        }
        // 用户名密码都正确
        return user;
    }
}
